#pragma once

#include <osg/ref_ptr>
#include <osg/Referenced>
#include <osg/PolygonMode>
#include <osgViewer/Viewer>
#include <osgViewer/CompositeViewer>
#include <osgGA/GUIEventAdapter>
#include <osg/CullFace> 
#include <osg/Multisample>
#include <QtGui/QKeyEvent>
#include <QtOpenGL/QGLWidget>
#include <QtCore/qnamespace.h>
#include <QtCore/qtimer.h>
#include "publicgui_global.h"
#include "base.h"
#include "viewer.h"
#include "scene.h"
#include <osgGA/CameraManipulator>
#include <osgQt/GraphicsWindowQt>

namespace VrDataCore
{
	class Drawable;
}

namespace ControlCenter
{
	class ManipulatorManager;
}

namespace PublicGui
{
	class PUBLICGUI_EXPORT AdapterWidget : public osgQt::GLWidget,osgViewer::CompositeViewer
	{
		Q_OBJECT
	public:
		AdapterWidget(const QGLFormat& format,QWidget* parent,Qt::WindowFlags f,const QGLWidget* shareWidget = NULL,bool forwardKeyEvents = true);
		virtual ~AdapterWidget();
		osgViewer::GraphicsWindow* getGraphicsWindow();
		void enableActiveView( bool enable );
		virtual void restoreSurface(){}
		ControlCenter::ManipulatorManager* getManipulatorManager();
		virtual void frame(double simulationTime=USE_REFERENCE_TIME);
	protected:
		void init();
		void testViewChange();
		virtual void resizeEvent(QResizeEvent* event);
		virtual void initializeGL();
		 virtual void glDraw();
		osg::ref_ptr<osgViewer::Viewer>						_viewer;
		QTimer												_timer;
		bool												_isActiveView;
		ControlCenter::ManipulatorManager*					_manipulatorManager;
		//osg::ref_ptr<CoreBase::Scene>						_mainScene ;
		bool												_breakToolTip;
		std::vector<CoreBase::Viewer*>						_viewerVector;
	};

	class PUBLICGUI_EXPORT SceneViewer : public AdapterWidget
	{
	public:
		SceneViewer(const QGLFormat& format,int viewtype,QWidget* parent,Qt::WindowFlags f = 0);
		virtual ~SceneViewer();
		virtual void setScene(CoreBase::Scene* scene,CoreBase::Viewer* viewer = NULL);
		CoreBase::Scene* getScene(CoreBase::Viewer* viewer = NULL) const;
		virtual void addIntoScene(CoreBase::Drawable* drawable,CoreBase::Viewer* viewer);
		virtual void delFrmScene(CoreBase::Drawable* drawable,CoreBase::Viewer* viewer);
		virtual void initializeGL();
		virtual void setManipulator(osgGA::CameraManipulator* mani); 
		void setClearColor( float r, float g, float b, float a );
		void setClearImage(const std::string& imagename);
		void setPolygonMode( int mode=0, int face=0 );
		void setCullFace(int mode = 0,bool enable = true);
		//HTSDSelection* getSelection() const
		//{
		//	if( _selection.valid() )
		//		return _selection.get() ;
		//	return NULL ;
		//}
		//void setSelection( HTSDSelection* sel, ModelPropertyType deftarget )
		//{
		//	if( sel->getViewer() != this )
		//	{
		//		return ;
		//	}
		//	_selection = sel ;
		//	HTSDModManager::GetInstance().setCurrentTargetType( deftarget ) ;
		//}
		//void clearSelection()
		//{
		//	_selection = NULL ;
		//}
		//virtual bool getSceneLocation( float x, float y,  ModelMatrix&matrix )
		//{
		//	return false ;
		//} 
		virtual void setProjectionMatrixAsOrtho(double left, double right, double bottom, double top,
															double zNear, double zFar)
		{
			_viewer->getCamera()->setProjectionMatrixAsOrtho( left, right, bottom, top, zNear, zFar ) ;
		}
		virtual void setProjectionMatrixAsOrtho2D(double left, double right, 	double bottom, double top)
		{
			_viewer->getCamera()->setProjectionMatrixAsOrtho2D( left, right, bottom, top ) ;
		}
		virtual void setProjectionMatrixAsFrustum(double left, double right, double bottom, double top, double zNear, double zFar)
		{
			_viewer->getCamera()->setProjectionMatrixAsFrustum( left, right, bottom, top, zNear, zFar ) ;
		}
		virtual void setProjectionMatrixAsPerspective(double fovy,double aspectRatio, double zNear, double zFar)
		{
			_viewer->getCamera()->setProjectionMatrixAsPerspective( fovy, aspectRatio, zNear, zFar ) ;
		}

		osg::Matrixd getProjectionMatrix() const
		{
			return _viewer->getCamera()->getProjectionMatrix() ;
		}

		osg::Matrixd getViewMatrix()
		{
			return _viewer->getCameraManipulator()->getInverseMatrix() ;
		}

		osg::Matrixd getInverseViewMatrix()
		{
			return _viewer->getCameraManipulator()->getMatrix() ;
		}
		//void slotCameraReset(  HTSDQtViewer* view ) ;
		//void slotLockEyeView( bool enable ) ;
		//void slotSpeedChanged(int speed);
		//void setRoamManipulator(bool isroam);
		//std::vector<osg::Vec3> getRoamStartVec() const;
		//CameraInfo getCameraInfo() ;
		//void setRoamStartVec(std::vector<osg::Vec3> vec) const;
protected:
		osg::Matrix getMVPWMatrix();
		void  initGlobalState( ) ;
		int											_viewType;
		//virtual void cameraReset() ;
		//void createBgCamera() ;
		//osg::ref_ptr< HTSDSelection >_selection ;
		//osg::ref_ptr< commonCuller > _culler ;
		//osg::ref_ptr<osg::Camera> _bgcamera ;
	};
}